﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Yonkly.Utility;
using Yonkly.Controllers;

namespace Yonkly.Models
{
    public interface IUserItem
    {
        string UserName { get; set; }
        Guid UserId { get; set; }
        string Email { get; set; }
    }

    public class UserItem : IUserItem
    {
        private string about;
        public string About
        {
            get
            {
                if (string.IsNullOrEmpty(about))
                {
                    using (var dc = new YonklyDataContext())
                    {
                        about = ProfileController.GetUserSettings(UserId).About; // dc.UserSettings.First(u => u.UserId == UserId).About;
                    }
                }
                return about;
            }
        }

        private string url;
        public string Url
        {
            get
            {
                if (string.IsNullOrEmpty(url))
                {
                    using (var dc = new YonklyDataContext())
                    {
                        url = ProfileController.GetUserSettings(UserId).Url;// dc.UserSettings.First(u => u.UserId == UserId).Url;
                    }
                }
                return url;
            }
        }

        public string UserName
        {
            get;
            set;
        }

        public Guid UserId { get; set; }

        public string Email
        {
            get;
            set;
        }

        public List<Group> Groups
        {
            get
            {
                List<Group> groups;
                using (var dc = new YonklyDataContext())
                {
                    groups = (from mem in dc.GroupMembers
                              where mem.UserId == UserId
                              select mem.Group).ToList();
                }
                return groups;
            }
        }

        public List<Group> SidebarGroups
        {
            get
            {
                List<Group> sidebarGroups;
                using (var dc = new YonklyDataContext())
                {
                    sidebarGroups = (from mem in dc.GroupMembers
                                     where mem.UserId == UserId
                                     && mem.Status == (int)GroupMemberStatuses.Approved
                                     select mem.Group).Take(10).ToList();
                }
                return sidebarGroups;
            }
        }

        public static UserItem CreateUserItem(string username)
        {
            MembershipUser user = System.Web.Security.Membership.GetUser(username);
            if (user != null)
            {
                var userid = (Guid)user.ProviderUserKey;
                var useritem = new UserItem { UserName = username, Email = user.Email, UserId = userid };
                return useritem;
            }
            return null;
        }
        private string twitterusername;
        public string TwitterUserName
        {
            get
            {
                if (string.IsNullOrEmpty(twitterusername))
                {
                    using (var dc = new YonklyDataContext())
                    {
                        twitterusername = ProfileController.GetUserSettings(UserId).TwitterUserName; // dc.UserSettings.First(u => u.UserId == UserId).About;
                    }
                }
                return twitterusername;
            }
        }

        private string twitterpassword;
        public string TwitterPassword
        {
            get
            {
                if (string.IsNullOrEmpty(twitterpassword))
                {
                    using (var dc = new YonklyDataContext())
                    {
                        twitterpassword = ProfileController.GetUserSettings(UserId).TwitterPassword; // dc.UserSettings.First(u => u.UserId == UserId).About;
                    }
                }
                return twitterpassword;
            }
        }
    }

    public static class IUserItemExtensions
    {
        public static string Gravatar(this IUserItem useritem)
        {
            return Utils.GetGravatar(useritem.Email);
        }
    }


    public class GroupUserItem : IUserItem
    {
        public string UserName { get; set; }
        public Guid UserId { get; set; }
        public string Email { get; set; }
        public GroupMemberRoles Role { get; set; }
        public GroupMemberStatuses Status { get; set; }

        public long GroupId { get; set; }
    }
}